exports.GameLoader=  Core.Class.subclass( {
	
	classname : 'GameLoader',
	
	initialize: function()
	{
		//NgLogE("initializing GameLoader");
		this._timeDifference = null;
		this._isSyncSuccess = true;
	},
	
	synchronizeServerTime: function(showProgress){
		//NgLogE("synchronizeServerTime GameLoader");
		this._isSyncSuccess = false;
		this.areTimesSynchronized = false;
		
		if (showProgress){
			this._showResyncProgress = true;
			UIFactory.showProgressHUD(Core.Localization.getString("resumeGame"));
		}	
		else{
			this._showResyncProgress = false;
		}
		
		this._webFetcher = new GAWebFetcher(this, 30, 0);
		this._webFetcher.invokeGetRequest('http://svn-133.ngtaptownservice.appspot.com/getServerTime');
	},
	
	webFetchSuccess : function(webFetcher, responseData){
		//NgLogE("webFetchSuccess GameLoader");
		this._isSyncSuccess = true;
		if(webFetcher != null){
			webFetcher.destroy();
			webFetcher = null;
		}
		
		this._responseObject = eval("(" + responseData + ")");
		
		if (this._showResyncProgress){
			UIFactory.hideProgressHUD();
			//UIFactory.showToast('Time Synchronized with Server');
		}
		UIFactory.hideBlockView();
		this.resynchronizeTimes();
	},
	
	webFetchFailure : function(webFetcher, errorCode){
		//NgLogE("webFetchFailure GameLoader error code = "+errorCode);
		
		if(webFetcher != null){
			webFetcher.destroy();
			webFetcher = null;
		}
		
		if (this._showResyncProgress){	
			UIFactory.hideProgressHUD();
			//UIFactory.showToast(Core.Localization.getString("syncFailure"));
		}
		UIFactory.hideBlockView();							 
		this._failureUIAlert = new GAUIAlertView(Core.Localization.getString("warning"),Core.Localization.getString("syncFailure"),this,null,Core.Localization.getString("retry"));
		this._failureUIAlert.setBackgroundImagePath("./Content/alert-background2.png");
		this._failureUIAlert.setOkImageNormalPath("./Content/alert-ok-normal.png");
		this._failureUIAlert.setOkImageSelectedPath("./Content/alert-ok-selected.png");
		this._failureUIAlert.setCancelImageNormalPath("./Content/alert-cancel-normal.png");
		this._failureUIAlert.setCancelImageSelectedPath("./Content/alert-cancel-selected.png");
		this._failureUIAlert.setTagValue(1111);
		this._failureUIAlert.show();
		
	},
	getIsSyncSuccess: function()
	{
		return this._isSyncSuccess;
	},  
	clickedButtonAtIndex: function(buttonIndex,alerWindow)
	{
		
		if (buttonIndex == 0) 
		{
			if(alerWindow.getTagValue() == 1111)
			{
				UIFactory.showBlockView();						 
				this.synchronizeServerTime(true);
			}
			
			
		} 
		else if (buttonIndex == 1)
		{
			//NgLogE("Cancel button clicked");
		}
		
	},
	
	
	getResponseObject : function (){
		return this._responseObject;
	},
	
	resynchronizeTimes: function(){
		var currentTime = (new Date().getTime()) / 1000;
		
		var newServerTime = parseInt(this.getResponseObject().servertime);
		//console.log('Resync: New Server Time = ' + newServerTime); 
		
		var newTimeDifference = newServerTime - currentTime;
		
		//console.log('Resync: Previous Time Difference = ' + this._timeDifference);
		//console.log('Resync: New Time Difference = ' + newTimeDifference);
		
		this.setTimeDifference(newTimeDifference);
		if(MMGameLayer)
		{
			//NgLogE("GameLoader::resetng MyUserLayer timeouts");
			MMGameLayer.getMyUserLayer().resetTimeouts();
		}
		
		this.areTimesSynchronized = true;
	},
	
	setTimeDifference : function(time){
		this._timeDifference = time;
		//NgLogE("MMGameState timeDiff before" + MMGameState._serverTimeDiff);
		MMGameState._serverTimeDiff = this._timeDifference;
		//NgLogE("MMGameState timeDiff after" + MMGameState._serverTimeDiff);
	},
	
	
	
});